﻿using MuXue.Zyiz.Template.Domain.Entity;
using MuXue.Zyiz.Template.Domain.IRepository;
using MuXue.Zyiz.Template.Domain.Model;
using MuXue.Zyiz.Template.Domain.Model.response;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 【Demo可删除】
/// </summary>
namespace MuXue.Zyiz.Template.Infrastructure.Repository
{
    public class ordersRepository : BaseRepository<cs_orders>, IordersRepository
    {
        public ordersRepository(IDbContext dbContext) : base(dbContext)
        {

        }

        /// <summary>
        /// 获取合同列表
        /// 字段比较少
        /// </summary>
        /// <param name="app_user_id">app用户id</param>
        /// <returns></returns>
        public async Task<List<SmallOrderResp>> QueryListSmallModel(string app_user_id)
        {
            List<SmallOrderResp> result = new List<SmallOrderResp>();
            string sql = @"select o.order_no,o.app_user_id,c.course_name,o.school_name,o.student_grade,c.course_subject,c.course_price,c.class_times,c.contract_type_id,c.contract_type_name,
o.trade_serial_no,o.order_status,payment_channel,o.payable_amount,o.real_amount,o.payment_status,o.create_time,o.payment_time
 from cs_orders o left join cs_order_course c on o.order_no = c.order_no
 where o.app_user_id =@app_user_id";
            SqlParameter[] parameters = {
                           new SqlParameter("@app_user_id",app_user_id)
                   };
            result= this.QueryBySql<SmallOrderResp>(sql, parameters).ToList();

            return result;

        }


        /// <summary>
        /// 获取合同列表
        /// </summary>
        /// <param name="app_user_id"></param>
        /// <param name="status">-1全部订单（排除已取消和转化为合同）； 0待付款；1已付款未转成合同；2已取消的订单；3申请退费，4退费成功；100已转成合同</param>
        /// <returns></returns>
        public async Task<List<SmallOrderResp>> QueryListSmallModel(string app_user_id,int status)
        {
            List<SmallOrderResp> result = new List<SmallOrderResp>();
            string sql = @"select  o.order_no,o.app_user_id,c.course_name,o.school_name,o.student_grade,c.course_subject,c.course_price,c.class_times,c.contract_type_id,c.contract_type_name,
o.trade_serial_no,o.order_status,payment_channel,o.payable_amount,o.real_amount,o.payment_status,o.create_time,o.payment_time
 from cs_orders o left join cs_order_course c on o.order_no = c.order_no
 where o.app_user_id =@app_user_id";
            if (status == -1)
            {
                sql += "  and ( (o.order_status =0 and  o.payment_status=0)  or o.order_status =2 )";
            }
            if (status == 0)
            {
                sql += " and o.order_status =0 and  o.payment_status=0 ";
            } 

            SqlParameter[] parameters = {
                           new SqlParameter("@app_user_id",app_user_id)
                   };
            result = this.QueryBySql<SmallOrderResp>(sql, parameters).ToList();

            return result;

        }



        public async Task<cs_orders>  Detail(string app_user_id, string order_no)
        {
            cs_orders result = new cs_orders();
            string sql = @"select  * from cs_orders  where  app_user_id =@app_user_id and order_no=@order_no";
            SqlParameter[] parameters = {
                           new SqlParameter("@app_user_id",app_user_id),
                             new SqlParameter("@order_no",order_no)
                   };
            result = this.QueryEntityBySql<cs_orders>(sql, parameters);

            return result;

        }

        


    }
}
